package org.eproject.core.well.delegate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eproject.core.hibernate.dao.IDao;
import org.eproject.core.hibernate.pojo.SHandle;
import org.eproject.core.hibernate.pojo.SModule;
import org.eproject.core.hibernate.pojo.SStation;
import org.eproject.core.hibernate.pojo.SUsers;
import org.eproject.core.well.exception.DeleGateException;
import org.eproject.core.well.exception.FactoryException;
import org.eproject.core.well.factory.Simplefactory;

public class UserDeleGate extends DeleGate{
	public List vaildate(String name,String passwd)throws DeleGateException{
		IDao idao = null;
		String hql = "select * from s_users u where u.uname=? and u.upasswd=?";
		Object[] p = new Object[2];
		p[0]=new String(name);
		p[1]=new String(passwd);
		try {
			idao = (IDao)Simplefactory.creatorBean("userdao");
			return idao.find(hql,p);
		} catch (FactoryException e) {
			e.printStackTrace();
			throw new DeleGateException(e);
		}
	}
	
	public Smodulelist getSmodule(int uid)throws DeleGateException{
		Smodulelist sl = new Smodulelist();
		IDao idao = null;
		try {
			idao = (IDao)Simplefactory.creatorBean("userdao");
			SUsers u = (SUsers)idao.load(SUsers.class,uid);
			Set<SStation> uset = u.getSStations();
			for(SStation sta : uset){
				Set<SModule> stset = sta.getSModules();
				for(SModule sm : stset){
					int tmp = sm.getMlevel();
					if(tmp==1)sl.addOne(sm);
					if(tmp==2)sl.addTwo(sm);
					if(tmp==3)sl.addThir(sm);
				}
			}
			return sl;
		} catch (FactoryException e) {
			e.printStackTrace();
			throw new DeleGateException(e);
		}
	}
	
	public List getShandle(int uid,int mid)throws DeleGateException{
		IDao idao = null;
		try {
			idao = (IDao)Simplefactory.creatorBean("userdao");
			List<SHandle> slist=new ArrayList<SHandle>();
			SUsers u = (SUsers)idao.load(SUsers.class,uid);
			Set<SStation> uset = u.getSStations();
			for(SStation sta : uset){
				Set<SHandle> sh = sta.getSHandles();
				for(SHandle h : sh){
					SModule sm = h.getSModule();
					if(sm.getMid()==mid)slist.add(h);
				}
			}
			return slist;
		} catch (FactoryException e) {
			e.printStackTrace();
			throw new DeleGateException(e);
		}
	}
	
	public List getByPage(PageInfo pageinfo)throws DeleGateException{
		IDao idao = null;
		try {
			idao = (IDao)Simplefactory.creatorBean("userdao");
		} catch (FactoryException e) {
			e.printStackTrace();
			throw new DeleGateException(e);
		}
		List<SUsers> lu = idao.findPageByQuery("from SUsers",null,pageinfo);
		return lu;
	}
	
	public PageInfo initPageInfo(int defpagesize)throws DeleGateException{
		IDao idao = null;
		PageInfo pi = new PageInfo();
		pi.setPagecrre(1);
		pi.setPagesize(defpagesize);
		try {
			idao = (IDao)Simplefactory.creatorBean("userdao");
			pi.setPagecount((Integer)idao.getRecordCount());
			return pi;
		} catch (FactoryException e) {
			e.printStackTrace();
			throw new DeleGateException(e);
		}
	}
	
	public void addUser (Object o)throws DeleGateException{
		IDao idao = null;
		try {
			idao = (IDao)Simplefactory.creatorBean("userdao");
			idao.save(o);
		} catch (FactoryException e) {
			e.printStackTrace();
			throw new DeleGateException(e);
		}
	}
}
